package com.cybertron.ironhide.manage.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;

import com.cybertron.ironhide.manage.domain.SpiderField;
import com.cybertron.ironhide.manage.domain.SpiderModel;

public class SpiderCheckUtil {

	private static final Cache ehCache = (Cache) SpringUtil.getBean("ehCache");

	public static void check(SpiderModel model) throws MalformedURLException,
			IOException, XPatherException {
		String sourceUrl = model.getSourceUrl();
		if (StringUtils.isBlank(sourceUrl)) {
			return;
		}
		HtmlCleaner cleaner = new HtmlCleaner();
		TagNode root = cleaner.clean(new URL(model.getSourceUrl()));
		Object[] nodes = root.evaluateXPath(model.getTargetUrlXpath());
		Object[] nextNodes = root.evaluateXPath(model.getNextUrlXpath());
		if (nodes != null && nodes.length != 0) {
			model.setTargetUrlCount(nodes.length);
			model.setNextUrlCount(nextNodes.length);
			getFields(model, nodes);
		} else {
			return;
		}
	}

	private static void getFields(SpiderModel model, Object[] nodes)
			throws MalformedURLException, IOException, XPatherException {
		Object obj = nodes[0];
		String targetUrl = null;
		if (obj instanceof TagNode) {
			targetUrl = ((TagNode) obj).getAttributeByName("href");
		}
		if (targetUrl == null) {
			return;
		}
		HtmlCleaner cleaner = new HtmlCleaner();
		if (!StringUtils.startsWith(targetUrl, "http://")) {
			URL sourceUrl = new URL(model.getSourceUrl());
			targetUrl = "http://" + sourceUrl.getHost() + targetUrl;
		}
		TagNode root = cleaner.clean(new URL(targetUrl));
		List<SpiderField> fields = model.getFields();
		for (SpiderField field : fields) {
			field.setTargetUrl(targetUrl);
			String xpath = field.getXpath();
			String attr = field.getAttr();
			StringBuffer sb = new StringBuffer();
			Object[] fieldNodes = root.evaluateXPath(xpath);
			for (Object fieldNode : fieldNodes) {
				if (fieldNode instanceof TagNode) {
					if (StringUtils.isBlank(attr)) {
						sb.append("_" + ((TagNode) fieldNode).getText());
					} else {
						sb.append("_"
								+ ((TagNode) fieldNode)
										.getAttributeByName(attr));
					}
				} else if (xpath.endsWith("/text()")) {
					sb.append("_" + ((StringBuilder) fieldNode).toString());
				}
			}
			field.setVal(sb.toString());
		}
		ehCache.put(SubjectUtil.getSessionId(), model);
	}

	public static SpiderModel getModel() {
		Object obj = ehCache.get(SubjectUtil.getSessionId());
		SpiderModel model = null;
		if (obj != null && obj instanceof SimpleValueWrapper) {
			SimpleValueWrapper wrapper = ((SimpleValueWrapper) obj);
			Object val = wrapper.get();
			if (val != null && val instanceof SpiderModel) {
				model = (SpiderModel) val;
			}
		}
		return model;
	}
}
